<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="检验任务号" prop="checkTaskNumber">
        <el-input
          v-model="queryParams.checkTaskNumber"
          placeholder="请输入检验任务号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="检验类型" prop="dictInspectType">
        <el-select
          v-model="queryParams.dictInspectType"
          placeholder="请选择检验类型"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('dict_inspect_type')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检验员" prop="creatorName">
        <el-input
          v-model="queryParams.creatorName"
          placeholder="请输入检验员"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div style="width: 100%; height: 50px; display: flex; justify-content:flex-end">
      <div>
        <el-button
          type="primary"
          @click="openTableTitleSet"
        >
        表头设置
        </el-button>
      </div>
    </div>
     <!-- 列表 -->
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length?true:false"
      @select="selectionChange"
      @register="tableoperation"
      @update:currentPage="getTableData"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.pageNo"
      :tableHeight="tableHeight"
      >
      <template #action="{ row }">
        <cell>
          <el-button 
          link 
          type="primary" 
          @click="openForm('view', row.id)"
          v-hasPermi="['quality:defective-product-record:view']">
          查看
        </el-button>
        </cell>
      </template>
    </Table>
  </ContentWrap>
  <!-- 表头设置 -->
  <TableWidthSet ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="DefectiveProductRecord">
import {getIntDictOptions } from '@/utils/dict'
import * as DefectiveProductRecordApi from '@/api/quality/qualitycontrol/DefectiveProductRecord'
const loading = ref(true) // 列表的加载中
let  tableTitleList = reactive([]) // 表头的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  inspectionTaskCode: null,
  productCode: null,
  dictConfigQualityInspectType: null,
  customerName: null,
  supplierName: null,
  productName: null
})
const queryFormRef = ref() // 搜索的表单
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作getUserLabelPage
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject,tableMethods } = useTable({
  getListApi: DefectiveProductRecordApi.getDefectiveProductRecordPage, // 分页接口
  defaultParams:queryParams
})
//获取表格操作
const { getList} = tableMethods

/** 查询列表 */
const getTableData = (val) => {
  tableObject.currentPage = val
  getList()
}

/**表头数据 */
const getLabelAll = async () => {
  loading.value = true
  try {
    const data = await DefectiveProductRecordApi.getDefectiveProductRecordPage(queryParams)
    const {allSchemas} = useCrudSchemas(data.labels)
    allSchemas.tableColumns.push({
      label: '操作',
        field: 'action',
        isDetail: false,
        width: 80
    })
    tableTitleList = allSchemas.tableColumns;
    /**表头修改需要刷新表格 */
    getList()
  } finally {
    loading.value = false
  }
}

/**表头内部方法 */
const usTableoperation = ref()
const tableoperation = (table) => {
  usTableoperation.value = table
}

/**多选表头数据 */
const selectList = ref([])
const selectionChange = (data) => {
  selectList.value = data
}

/**打开表格表头设置**/
const tableTitleSet = ref() 
const openTableTitleSet = () => {
  const arr = tableTitleList.slice(0,-1)
  let code = 'DefectiveProductRecord'
  tableTitleSet.value.open(arr, code)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  tableObject.params = queryParams
  getList()
}

/** 查看详情*/
const { push } = useRouter() // 路由
const openForm = (type,id) => {
  push({
    path:'/quality/quality/qualitycontrol/defectiveProductRecordFrom',
    query: {
      type,
      id
    }
  })
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  await getLabelAll()
  getList()
})
</script>
